package com.geyue.controller;

import com.geyue.common.toolbox.result.Res;
import com.geyue.core.model.Integral;
import com.geyue.core.model.IntegralFor;
import com.geyue.core.model.User;
import com.geyue.core.service.CardService;
import com.geyue.core.service.IntegralForService;
import com.geyue.core.service.IntegralService;
import com.geyue.core.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;

/**
 * Created by Lyon on 2017/5/11.
 */
@Controller
@RequestMapping("/integral")
public class IntegralController {

    @Autowired
    private UserService userService;
    @Autowired
    private IntegralForService integralForService;
    @Autowired
    private IntegralService integralService;
    @Autowired
    private CardService cardService;

    @RequestMapping(value = "/",method = RequestMethod.GET)
    public String integral(Model model, HttpSession session){

        User userInfo = (User)session.getAttribute("user");
        //如果未登录或session过期
        if(userInfo == null)
            return "login";

        model.addAttribute("userInfo",userService.getOneByKey(userInfo.getId()));
        model.addAttribute("goods",new ArrayList<String>());
        return "integral";
    }

    @RequestMapping(value = "/for",method = RequestMethod.GET)
    public String for_(Model model,HttpSession session){

        User userInfo = (User)session.getAttribute("user");
        //如果未登录或session过期
        if(userInfo == null)
            return "login";

        model.addAttribute("userInfo",userInfo);
        return "integral_for";
    }

    @RequestMapping(value = "/for",method = RequestMethod.POST)
    @ResponseBody
    public Res for_(@Validated IntegralFor integral, BindingResult binding){

        if(binding.hasErrors())
            return Res.fail("参数错误!");

        User param1 = new User();
        param1.setMobile(integral.getMobile());
        User self = userService.getOne(param1);
        if(self.getIntegral() < integral.getIntegral())
           return Res.fail("积分不足!");

        User param = new User();
        param.setMobile(integral.getForMobile());
        if(userService.getOne(param) == null)
           return Res.fail("转移目标不存在!");

        integral.setIsOk(IntegralFor.submit);
        if(integralForService.saveNotEmpty(integral) > 0)
            return Res.success("提交成功!");

        return Res.fail("提交失败!");
    }

    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public String list(@PathVariable Integer id,HttpSession session,Model model){
        User userInfo = (User)session.getAttribute("user");
        //如果未登录或session过期
        if(userInfo == null)
            return "login";

//        model.addAttribute("data",integralService.orderByTime(id));
        model.addAttribute("uid",id);
        return "integral_list";
    }

    @RequestMapping(value = "/list",method = RequestMethod.POST)
    @ResponseBody
    public Res list(Integer uid,Integer year,Integer quarter, HttpSession session) {

        User userInfo = (User) session.getAttribute("user");
        //如果未登录或session过期
        if (userInfo == null)
            return Res.fail("用户信息过期,请重新登录!");

        return Res.success("",integralService.orderByTime(uid,year,quarter));
    }

    @RequestMapping(value = "/rp",method = RequestMethod.GET)
    public String rp(Model model,HttpSession session){
        User userInfo = (User)session.getAttribute("user");
        //如果未登录或session过期
        if(userInfo == null)
            return "login";

        model.addAttribute("data",cardService.getRp());
        model.addAttribute("uid",userInfo.getId());
        return "rpcard";
    }

    @RequestMapping(value = "/jd",method = RequestMethod.GET)
    public String jd(Model model,HttpSession session){
        User userInfo = (User)session.getAttribute("user");
        //如果未登录或session过期
        if(userInfo == null)
            return "login";

        model.addAttribute("uid",userInfo.getId());
        model.addAttribute("data",cardService.getJd());
        return "jdcard";
    }

    @RequestMapping(value = "/exchange",method = RequestMethod.POST)
    @ResponseBody
    public Res exchange(Integral integral, BindingResult binding){

        if(binding.hasErrors())
            return Res.fail("参数错误!");

        return integralService.exchange(integral);
    }

    @RequestMapping(value = "/receive",method = RequestMethod.POST)
    @ResponseBody
    public Res receive(Integer year,Integer quarter,HttpSession session){
        User userInfo = (User)session.getAttribute("user");
        //如果未登录或session过期
        if(userInfo == null)
            return Res.fail("用户信息过期,请重新登录!");

        return integralService.receive(year,quarter,userInfo.getId());
    }

    @RequestMapping(value = "/info",method = RequestMethod.POST)
    @ResponseBody
    public Res info(String mobile){
        User param = new User();
        param.setMobile(mobile);
        User user = userService.getOne(param);
        if(user != null)
            return Res.success("",user.getName());

        return Res.fail("用户不存在!");
    }

}
